package com.ruoyi.web.controller.car;

import com.ruoyi.car.domain.Car;
import com.ruoyi.car.service.ICarService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysFiles;
import com.ruoyi.system.service.ISysFilesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆Controller
 *
 * @author zls
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/car/car")
public class CarController extends BaseController {
    private String prefix = "car/car";

    @Resource
    private ICarService carService;
    @Autowired
    private ServerConfig serverConfig;
    @Resource
    private ISysFilesService sysFilesService;

    @RequiresPermissions("car:car:view")
    @GetMapping()
    public String car() {
        return prefix + "/car";
    }

    /**
     * 查询车辆列表
     */
    @RequiresPermissions("car:car:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Car car) {
        startPage();
        List<Car> list = carService.selectCarList(car);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表
     */
    @RequiresPermissions("car:car:export")
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Car car) {
        List<Car>      list = carService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        return util.exportExcel(list, "car");
    }

    /**
     * 新增车辆
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆
     */
    @RequiresPermissions("car:car:add")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Car car) {
        return toAjax(carService.insertCar(car));
    }

    /**
     * 修改车辆
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        SysFiles sysFiles = new SysFiles();
        sysFiles.setBizId(car.getId());
        sysFiles.setBizType("car_info");
        car.setIntroductionImg(sysFilesService.selectSysFilesList(sysFiles));
//        car.getIntroductionImg().parallelStream().forEach(item -> item.setUrl(serverConfig.getUrl() + item.getUrl()));
        sysFiles.setBizType("car_info_file");
        car.setAnnex(sysFilesService.selectSysFilesList(sysFiles));
//        car.getAnnex().parallelStream().forEach(item -> item.setUrl(serverConfig.getUrl() + item.getUrl()));
        return prefix + "/edit";
    }

    @GetMapping("/canshu/dlxt/{id}")
    public String dlxt(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/dlxt";
    }

    @GetMapping("/canshu/anquan/{id}")
    public String anquan(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/anquan";
    }

    @GetMapping("/canshu/gnpz/{id}")
    public String gnpz(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/gnpz";
    }

    @GetMapping("/canshu/xuanjia/{id}")
    public String xuanjia(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/xuanjia";
    }

    @GetMapping("/canshu/chicun/{id}")
    public String chicun(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/chicun";
    }

    @GetMapping("/canshu/zhongliang/{id}")
    public String zhongliang(@PathVariable("id") Long id, ModelMap mmap) {
        Car car = carService.selectCarById(id);
        mmap.put("car", car);
        return prefix + "/canshu/zhongliang";
    }

    /**
     * 修改保存车辆
     */
    @RequiresPermissions("car:car:edit")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Car car) {
        return toAjax(carService.updateCar(car));
    }

    /**
     * 删除车辆
     */
    @RequiresPermissions("car:car:remove")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carService.deleteCarByIds(ids));
    }

    /**
     * 查看车辆
     */
    @GetMapping("/edit_tree/{id}")
    public String edit_tree(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("carId", id);
        mmap.put("defaultUrl", "/car/car/edit/" + id);
        return prefix + "/edit_tree";
    }
    /**
     * 查看整车数据
     */
    @GetMapping("/edit_all_tree/{id}")
    public String edit_all_tree(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("carId", id);
        mmap.put("defaultUrl", "/car/door/edit/" + id);
        return prefix + "/edit_data_tree";
    }

    /**
     * 加载车辆列表树
     */
    @GetMapping("/treeData/{carId}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable("carId") Long carId) {
        List<Ztree> ztrees  = new ArrayList<Ztree>();

        Ztree       ztree_1 = new Ztree();
        ztree_1.setId(1L);
        ztree_1.setpId(0L);
        ztree_1.setName("车辆信息");
        ztree_1.setTitle("车辆信息");
        ztree_1.setOpen(true);
        ztrees.add(ztree_1);

        Ztree ztree_1_2 = new Ztree();
        ztree_1_2.setId(2L);
        ztree_1_2.setpId(1L);
        ztree_1_2.setName("车辆介绍");
        ztree_1_2.setTitle("车辆介绍");
        JSONObject ztree_data_1_2 = new JSONObject();
        ztree_data_1_2.put("url", "/car/car/edit/" + carId);
        ztree_1_2.setData(ztree_data_1_2);
        ztrees.add(ztree_1_2);

        Ztree ztree_1_3 = new Ztree();
        ztree_1_3.setId(3L);
        ztree_1_3.setpId(1L);
        ztree_1_3.setName("车辆参数配置");
        ztree_1_3.setTitle("车辆参数配置");
        ztrees.add(ztree_1_3);
        Ztree ztree_1_3_0 = new Ztree();
        ztree_1_3_0.setId(30L);
        ztree_1_3_0.setpId(3L);
        ztree_1_3_0.setName("动力系统数据");
        ztree_1_3_0.setTitle("动力系统数据");
        JSONObject ztree_data_1_3_0 = new JSONObject();
        ztree_data_1_3_0.put("url", "/car/car/canshu/dlxt/" + carId);
        ztree_1_3_0.setData(ztree_data_1_3_0);
        ztrees.add(ztree_1_3_0);
        Ztree ztree_1_3_1 = new Ztree();
        ztree_1_3_1.setId(31L);
        ztree_1_3_1.setpId(3L);
        ztree_1_3_1.setName("功能配置");
        ztree_1_3_1.setTitle("功能配置");
        JSONObject ztree_data_1_3_1 = new JSONObject();
        ztree_data_1_3_1.put("url", "/car/car/canshu/gnpz/" + carId);
        ztree_1_3_1.setData(ztree_data_1_3_1);
        ztrees.add(ztree_1_3_1);
        Ztree ztree_1_3_2 = new Ztree();
        ztree_1_3_2.setId(32L);
        ztree_1_3_2.setpId(3L);
        ztree_1_3_2.setName("安全参数");
        ztree_1_3_2.setTitle("安全参数");
        JSONObject ztree_data_1_3_2 = new JSONObject();
        ztree_data_1_3_2.put("url", "/car/car/canshu/anquan/" + carId);
        ztree_1_3_2.setData(ztree_data_1_3_2);
        ztrees.add(ztree_1_3_2);
        Ztree ztree_1_3_3 = new Ztree();
        ztree_1_3_3.setId(33L);
        ztree_1_3_3.setpId(3L);
        ztree_1_3_3.setName("悬架系统");
        ztree_1_3_3.setTitle("悬架系统");
        JSONObject ztree_data_1_3_3 = new JSONObject();
        ztree_data_1_3_3.put("url", "/car/car/canshu/xuanjia/" + carId);
        ztree_1_3_3.setData(ztree_data_1_3_3);
        ztrees.add(ztree_1_3_3);
        Ztree ztree_1_3_4 = new Ztree();
        ztree_1_3_4.setId(34L);
        ztree_1_3_4.setpId(3L);
        ztree_1_3_4.setName("车辆尺寸参数");
        ztree_1_3_4.setTitle("车辆尺寸参数");
        JSONObject ztree_data_1_3_4 = new JSONObject();
        ztree_data_1_3_4.put("url", "/car/car/canshu/chicun/" + carId);
        ztree_1_3_4.setData(ztree_data_1_3_4);
        ztrees.add(ztree_1_3_4);
        Ztree ztree_1_3_5 = new Ztree();
        ztree_1_3_5.setId(35L);
        ztree_1_3_5.setpId(3L);
        ztree_1_3_5.setName("车辆重量数据");
        ztree_1_3_5.setTitle("车辆重量数据");
        JSONObject ztree_data_1_3_5 = new JSONObject();
        ztree_data_1_3_5.put("url", "/car/car/canshu/zhongliang/" + carId);
        ztree_1_3_5.setData(ztree_data_1_3_5);
        ztrees.add(ztree_1_3_5);

        Ztree ztree_1_4 = new Ztree();
        ztree_1_4.setId(4L);
        ztree_1_4.setpId(1L);
        ztree_1_4.setName("整车测量数据");
        ztree_1_4.setTitle("整车测量数据");
        ztrees.add(ztree_1_4);
        Ztree ztree_1_4_0 = new Ztree();
        ztree_1_4_0.setId(40L);
        ztree_1_4_0.setpId(4L);
        ztree_1_4_0.setName("外围测量");
        ztree_1_4_0.setTitle("外围测量");
        ztrees.add(ztree_1_4_0);
        Ztree ztree_1_4_1 = new Ztree();
        ztree_1_4_1.setId(41L);
        ztree_1_4_1.setpId(4L);
        ztree_1_4_1.setName("内部测量");
        ztree_1_4_1.setTitle("内部测量");
        ztrees.add(ztree_1_4_1);
        Ztree ztree_1_4_2 = new Ztree();
        ztree_1_4_2.setId(42L);
        ztree_1_4_2.setpId(4L);
        ztree_1_4_2.setName("车身间隙测量");
        ztree_1_4_2.setTitle("车身间隙测量");
        ztrees.add(ztree_1_4_2);
        Ztree ztree_1_4_3 = new Ztree();
        ztree_1_4_3.setId(43L);
        ztree_1_4_3.setpId(4L);
        ztree_1_4_3.setName("地盘测量");
        ztree_1_4_3.setTitle("地盘测量");
        ztrees.add(ztree_1_4_3);

        Ztree ztree_1_5 = new Ztree();
        ztree_1_5.setId(5L);
        ztree_1_5.setpId(1L);
        ztree_1_5.setName("整车图片概述");
        ztree_1_5.setTitle("整车图片概述");
        ztrees.add(ztree_1_5);
        Ztree ztree_1_5_0 = new Ztree();
        ztree_1_5_0.setId(50L);
        ztree_1_5_0.setpId(5L);
        ztree_1_5_0.setName("外观图片");
        ztree_1_5_0.setTitle("外观图片");
        ztrees.add(ztree_1_5_0);
        Ztree ztree_1_5_1 = new Ztree();
        ztree_1_5_1.setId(51L);
        ztree_1_5_1.setpId(5L);
        ztree_1_5_1.setName("内饰图片");
        ztree_1_5_1.setTitle("内饰图片");
        ztrees.add(ztree_1_5_1);
        Ztree ztree_1_5_2 = new Ztree();
        ztree_1_5_2.setId(52L);
        ztree_1_5_2.setpId(5L);
        ztree_1_5_2.setName("照明");
        ztree_1_5_2.setTitle("照明");
        ztrees.add(ztree_1_5_2);
        Ztree ztree_1_5_3 = new Ztree();
        ztree_1_5_3.setId(53L);
        ztree_1_5_3.setpId(5L);
        ztree_1_5_3.setName("指示灯");
        ztree_1_5_3.setTitle("指示灯");
        ztrees.add(ztree_1_5_3);
        Ztree ztree_1_5_4 = new Ztree();
        ztree_1_5_4.setId(54L);
        ztree_1_5_4.setpId(5L);
        ztree_1_5_4.setName("底盘图片");
        ztree_1_5_4.setTitle("底盘图片");
        ztrees.add(ztree_1_5_4);

        Ztree ztree_1_6 = new Ztree();
        ztree_1_6.setId(6L);
        ztree_1_6.setpId(1L);
        ztree_1_6.setName("整车亮点分析");
        ztree_1_6.setTitle("整车亮点分析");
        ztrees.add(ztree_1_6);
        return ztrees;
    }

    /**
     * 加载整车数据列表树
     */
    @GetMapping("/treeAllData/{carId}")
    @ResponseBody
    public List<Ztree> treeAllData(@PathVariable("carId") Long carId) {
        List<Ztree> ztrees  = new ArrayList<Ztree>();
        Ztree       ztree_1 = new Ztree();
        ztree_1.setId(1L);
        ztree_1.setpId(0L);
        ztree_1.setName("整车外围");
        ztree_1.setTitle("整车外围");
        ztree_1.setOpen(true);
        ztrees.add(ztree_1);
        Ztree ztree_1_0 = new Ztree();
        ztree_1_0.setId(10L);
        ztree_1_0.setpId(1L);
        ztree_1_0.setName("关闭系统");
        ztree_1_0.setTitle("关闭系统");
        JSONObject ztree_data_1_0 = new JSONObject();
        ztree_data_1_0.put("url", CarImportController.url_imp + "/" + carId + "/" + ztree_1_0.getId());
        ztree_1_0.setData(ztree_data_1_0);
        ztrees.add(ztree_1_0);
        Ztree ztree_1_1 = new Ztree();
        ztree_1_1.setId(11L);
        ztree_1_1.setpId(1L);
        ztree_1_1.setName("保险杠");
        ztree_1_1.setTitle("保险杠");
        JSONObject ztree_data_1_1 = new JSONObject();
        ztree_data_1_1.put("url", "/car/door/edit/" + carId);
        ztree_1_1.setData(ztree_data_1_1);
        ztrees.add(ztree_1_1);

        Ztree ztree_2 = new Ztree();
        ztree_2.setId(2L);
        ztree_2.setpId(0L);
        ztree_2.setName("整车内部");
        ztree_2.setTitle("整车内部");
        ztrees.add(ztree_2);

        Ztree ztree_2_0 = new Ztree();
        ztree_2_0.setId(20L);
        ztree_2_0.setpId(2L);
        ztree_2_0.setName("座椅系统");
        ztree_2_0.setTitle("座椅系统");
        ztrees.add(ztree_2_0);

        Ztree ztree_3 = new Ztree();
        ztree_3.setId(3L);
        ztree_3.setpId(0L);
        ztree_3.setName("动力系统");
        ztree_3.setTitle("动力系统");
        ztrees.add(ztree_3);
        Ztree ztree_3_0 = new Ztree();
        ztree_3_0.setId(30L);
        ztree_3_0.setpId(3L);
        ztree_3_0.setName("发动机");
        ztree_3_0.setTitle("发动机");
        ztrees.add(ztree_3_0);
        Ztree ztree_3_1 = new Ztree();
        ztree_3_1.setId(31L);
        ztree_3_1.setpId(3L);
        ztree_3_1.setName("变速箱");
        ztree_3_1.setTitle("变速箱");
        ztrees.add(ztree_3_1);
        Ztree ztree_3_2 = new Ztree();
        ztree_3_2.setId(32L);
        ztree_3_2.setpId(3L);
        ztree_3_2.setName("悬挂系统");
        ztree_3_2.setTitle("悬挂系统");
        ztrees.add(ztree_3_2);

        Ztree ztree_4 = new Ztree();
        ztree_4.setId(4L);
        ztree_4.setpId(0L);
        ztree_4.setName("悬架系统");
        ztree_4.setTitle("悬架系统");
        ztrees.add(ztree_4);

        Ztree ztree_5 = new Ztree();
        ztree_5.setId(5L);
        ztree_5.setpId(0L);
        ztree_5.setName("附件系统");
        ztree_5.setTitle("附件系统");
        ztrees.add(ztree_5);

        Ztree ztree_6 = new Ztree();
        ztree_6.setId(6L);
        ztree_6.setpId(0L);
        ztree_6.setName("冷却系统");
        ztree_6.setTitle("冷却系统");
        ztrees.add(ztree_6);

        Ztree ztree_7 = new Ztree();
        ztree_7.setId(7L);
        ztree_7.setpId(0L);
        ztree_7.setName("空调系统");
        ztree_7.setTitle("空调系统");
        ztrees.add(ztree_7);

        Ztree ztree_8 = new Ztree();
        ztree_8.setId(8L);
        ztree_8.setpId(0L);
        ztree_8.setName("电器系统");
        ztree_8.setTitle("电器系统");
        ztrees.add(ztree_8);

        Ztree ztree_9 = new Ztree();
        ztree_9.setId(9L);
        ztree_9.setpId(0L);
        ztree_9.setName("整车底盘系统");
        ztree_9.setTitle("整车底盘系统");
        ztrees.add(ztree_9);
        return ztrees;
    }
}
